import { Node, Vec3 } from "cc";
import { GridSensorBase, ProcessCollidersMethod } from "./GridSensorBase";
import { SensorCompressionType } from "./CompressionSpec";



/**
 * @en
 *  Grid-based sensor that counts the number of detctable objects.
 * @zh
 * 基于网格的传感器，用于计算可检测对象的数量。
 */
export class CountingGridSensor extends GridSensorBase {
    /**
     * Create a CountingGridSensor with the specified configuration.
     * @param name The sensor name
     * @param cellScale The scale of each cell in the grid
     * @param gridSize Number of cells on each side of the grid
     * @param detectableTags Tags to be detected by the sensor
     * @param compression Compression type
     */
    constructor(
        name: string,
        cellScale: Vec3,
        gridSize: Vec3,
        detectableTags: string[],
        compression: SensorCompressionType
    ) {
        super(name, cellScale, gridSize, detectableTags, compression);
        this.compressionType = SensorCompressionType.None;
    }

    protected override getCellObservationSize(): number {
        return this.detectableTags == null ? 0 : this.detectableTags.length;
    }

    protected override isDataNormalized(): boolean {
        return false;
    }

    override getProcessCollidersMethod(): ProcessCollidersMethod {
        return ProcessCollidersMethod.ProcessAllColliders;
    }

    /**
     * Get object counts for each detectable tags detected in a cell.
     * @param detectedObject The game object that was detected within a certain cell
     * @param tagIndex The index of the detectedObject's tag in the DetectableObjects list
     * @param dataBuffer The buffer to write the observation values.
     *         The buffer size is configured by GetCellObservationSize.
     */
    protected override getObjectData(detectedObject: Node, tagIndex: number, dataBuffer: number[]): void {
        dataBuffer[tagIndex] += 1;
    }
}